<template>
  <div class="m-views-tpl">
    <div class="m-table">
      <vxe-grid v-if="crud.grids.grid" ref="xTable" v-bind="crud.grids.grid" @menu-click="contextMenuClickEvent" />
    </div>
    <form-modal @register="formRegisterModal" @success="crud.toQuery()" />
    <editor-modal @register="editorRegisterModal" @success="crud.toQuery()" />
    <settings-modal @register="settingsRegisterModal" @success="crud.toQuery()" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref } from 'vue';
  import { useModal } from '/@/components/Modal';

  import CRUD from '/@/crud/core/init';
  import * as _API from './api';
  import { VxeGridInstance, VxeGridProps, VxeTableEvents } from 'vxe-table';
  import { columns, contextMenu } from './config';
  import formModal from './modal/edit/index.vue';
  import editorModal from './modal/editor/index.vue';
  import settingsModal from './modal/settings/index.vue';
  export default defineComponent({
    name: 'MenuManagement',
    components: { formModal, editorModal, settingsModal },
    setup() {
      const crud = CRUD({ title: '生成模板', crudMethod: { ..._API } }); // 初始化crud
      const xTable = ref({} as VxeGridInstance);
      crud.grids.grid = {
        columns,
        data: [],
        height: '100%',
        treeConfig: { rowField: 'name' },
        menuConfig: contextMenu(xTable),
      } as VxeGridProps;
      onMounted(() => crud.toQuery());
      const [formRegisterModal, formModalMethods] = useModal();
      const [editorRegisterModal, editorModalMethods] = useModal();
      const [settingsRegisterModal, settingsMethods] = useModal();
      /**
       * @vuese
       * @Description: 右键
       * @arg {*} menu 菜单
       * @arg {*} row 行
       */
      const contextMenuClickEvent: VxeTableEvents.MenuClick = ({ menu, row }) => {
        const $table = xTable.value;
        switch (menu.code) {
          // 展开
          case 'expand':
            $table.setTreeExpand(row, true);
            break;
          // 收起
          case 'contract':
            $table.setTreeExpand(row, false);
            break;
          // 删除
          case 'del':
            crud.toDel({
              params: { name: row.name, path: (row.type !== 0 && row.path) || undefined },
              api: (row.type !== 0 && _API.delFileAPI) || null,
              modal: formModalMethods,
            });
            break;
          case 'add':
            if (!row) {
              formModalMethods.openModal(true);
            } else {
              editorModalMethods.openModal(true, { record: row });
            }
            break;
          case 'edit':
            crud.toEdit({
              after: ({ res }) => editorModalMethods.openModal(true, { record: res.data.result, isUpdate: true }),
              params: { path: row.path },
            });
            break;
          case 'rename':
            formModalMethods.openModal(true, { record: row, isUpdate: true });
            break;
          case 'settings':
            crud.toDetail({
              api: _API.getSettingsAPI,
              params: { name: row.name },
              after: ({ res }) => settingsMethods.openModal(true, { record: res.data.result, isUpdate: true }),
              // || crud.changeModal({ modal: settingsMethods, visible: true, params: { record: res.data.result } }),
            });
            break;
        }
      };

      return {
        contextMenuClickEvent,
        formRegisterModal,
        editorRegisterModal,
        settingsRegisterModal,
        xTable,
        crud,
      };
    },
  });
</script>
<style lang="less">
  .m-views-tpl {
    padding: 10px;
    height: 100%;
    box-sizing: border-box;
    > .m-table {
      height: 100%;
    }
  }
  [data-theme='dark'] {
    .contextMenu {
      background-color: #151515;
      a {
        color: #fff;
      }
      .link--disabled {
        a {
          color: rgba(255, 255, 255, 0.3) !important;
        }
      }
    }
  }
</style>
